<template>
  <div class="berth-management">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="停车场">
          <el-select v-model="searchForm.lotId" placeholder="请选择停车场">
            <el-option
              v-for="item in parkingLots"
              :key="item.lotId"
              :label="item.lotName"
              :value="item.lotId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="泊位编号">
          <el-input v-model="searchForm.spotCode" placeholder="请输入泊位编号"></el-input>
        </el-form-item>
        <el-form-item label="泊位类型">
          <el-select v-model="searchForm.spotType" placeholder="请选择泊位类型">
            <el-option label="普通" value="1"></el-option>
            <el-option label="充电" value="2"></el-option>
            <el-option label="障碍" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮 -->
    <div class="operation-bar">
      <el-button type="primary" @click="handleAdd">新增泊位</el-button>
    </div>

    <!-- 数据表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="spotId" label="泊位ID" width="100"></el-table-column>
      <el-table-column prop="spotCode" label="泊位编号" width="120"></el-table-column>
      <el-table-column prop="lotName" label="停车场" width="150"></el-table-column>
      <el-table-column prop="spotType" label="泊位类型" width="100">
        <template slot-scope="scope">
          {{ getSpotTypeName(scope.row.spotType) }}
        </template>
      </el-table-column>
      <el-table-column prop="isOccupied" label="占用状态" width="100">
        <template slot-scope="scope">
          {{ scope.row.isOccupied === '1' ? '已占用' : '空闲' }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template slot-scope="scope">
          {{ scope.row.status === '1' ? '停用' : '正常' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total">
      </el-pagination>
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="停车场" prop="lotId">
          <el-select v-model="form.lotId" placeholder="请选择停车场">
            <el-option
              v-for="item in parkingLots"
              :key="item.lotId"
              :label="item.lotName"
              :value="item.lotId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="泊位编号" prop="spotCode">
          <el-input v-model="form.spotCode" placeholder="请输入泊位编号"></el-input>
        </el-form-item>
        <el-form-item label="泊位类型" prop="spotType">
          <el-select v-model="form.spotType" placeholder="请选择泊位类型">
            <el-option label="普通" value="1"></el-option>
            <el-option label="充电" value="2"></el-option>
            <el-option label="障碍" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getParkSpotList, addParkSpot, updateParkSpot, deleteParkSpot } from '@/api/parkinglotmanagement/berthmanagement'
import { getLotById } from '@/api/parkinglotmanagement/parking'

export default {
  name: 'BerthManagement',
  data() {
    return {
      parkingLots: [],
      searchForm: {
        lotId: '',
        spotCode: '',
        spotType: ''
      },
      tableData: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      dialogVisible: false,
      dialogTitle: '',
      form: {
        spotId: '',
        lotId: '',
        spotCode: '',
        spotType: '',
        status: '0'
      },
      rules: {
        lotId: [
          { required: true, message: '请选择停车场', trigger: 'change' }
        ],
        spotCode: [
          { required: true, message: '请输入泊位编号', trigger: 'blur' }
        ],
        spotType: [
          { required: true, message: '请选择泊位类型', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async fetchParkingLots() {
      try {
        const response = await getLotById()
        this.parkingLots = response.data
      } catch (error) {
        console.error('获取停车场列表失败:', error)
      }
    },
    // 获取泊位类型名称
    getSpotTypeName(type) {
      const typeMap = {
        '1': '普通',
        '2': '充电',
        '3': '障碍'
      }
      return typeMap[type] || '未知'
    },
    // 查询
    handleSearch() {
      this.fetchData()
    },
    // 重置查询
    resetSearch() {
      this.searchForm = {
        lotId: '',
        spotCode: '',
        spotType: ''
      }
      this.fetchData()
    },
    // 获取数据
    fetchData() {
      const query = {
        ...this.searchForm,
        pageNum: this.pagination.currentPage,
        pageSize: this.pagination.pageSize
      }
      getParkSpotList(query).then(response => {
        this.tableData = response.data.records
        this.pagination.total = response.data.total
      })
    },
    // 新增
    handleAdd() {
      this.dialogTitle = '新增泊位'
      this.form = {
        spotCode: '',
        spotType: '',
        status: '0'
      }
      this.dialogVisible = true
    },
    // 编辑
    handleEdit(row) {
      this.dialogTitle = '编辑泊位'
      this.form = { ...row }
      this.dialogVisible = true
    },
    // 删除
    handleDelete(row) {
      this.$confirm('确认删除该泊位?', '提示', {
        type: 'warning'
      }).then(() => {
        deleteParkSpot({ spotId: row.spotId }).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      }).catch(() => {})
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const submitMethod = this.form.spotId ? updateParkSpot : addParkSpot
          submitMethod(this.form).then(() => {
            this.$message.success('保存成功')
            this.dialogVisible = false
            this.fetchData()
          })
        }
      })
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.fetchData()
    },
    // 当前页改变
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.fetchData()
    }
  },
  created() {
    this.fetchParkingLots()
    this.fetchData()
  }
}
</script>

<style scoped>
.berth-management {
  padding: 20px;
}
.search-bar {
  margin-bottom: 20px;
}
.operation-bar {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
